/*
 * 
 * 
 * 
 */
package com.hboxs.asl.controller.admin;

import com.hboxs.asl.Filter;
import com.hboxs.asl.Message;
import com.hboxs.asl.Order;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.entity.BankCard;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.service.BankCardService;
import com.hboxs.asl.service.MemberService;
import com.hboxs.asl.util.BankUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

import javax.annotation.Resource;

/**
 * Controller - 银行卡
 */
@Controller("adminBankCardController")
@RequestMapping("/asl-admin/bank_card")
public class BankCardController extends BaseController {

    @Resource(name = "bankCardServiceImpl")
    private BankCardService bankCardService;
    @Resource(name = "memberServiceImpl")
    private MemberService memberService;

    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model, HttpSession session) {
        model.addAttribute("members", memberService.findAllCommon());
        return "/admin/bank_card/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(BankCard bankCard, Long memberId, RedirectAttributes redirectAttributes) {
        if (!isValid(bankCard)) {
            return ERROR_VIEW;
        }
        Member member = memberService.find(memberId);
        if (member == null) {
            return ERROR_VIEW;
        }

        if (bankCardService.findBankCardByNumber(bankCard.getCardNumber()) != null) {
            addFlashMessage(redirectAttributes, Message.error("该银行卡已存在"));
            return "redirect:add.htm";
        }
        String code = BankUtils.validateAndCacheCardInfo(bankCard.getCardNumber());
        if (code == null) {
            addFlashMessage(redirectAttributes, Message.error("无法识别银行卡所属银行名称"));
            return "redirect:add.htm";
        }

        bankCard.setCode(code.toLowerCase());
        bankCard.setMember(member);
        bankCardService.save(bankCard);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model, HttpSession session) {
        model.addAttribute("bankCard", bankCardService.find(id));
        model.addAttribute("members", memberService.findAllCommon());
        return "/admin/bank_card/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(BankCard bankCard, Long memberId, RedirectAttributes redirectAttributes) {
        if (!isValid(bankCard)) {
            return ERROR_VIEW;
        }

        Member member = memberService.find(memberId);
        if (member == null) {
            return ERROR_VIEW;
        }


        String oldNumber = bankCardService.find(bankCard.getId()).getCardNumber();
        if (!bankCard.getCardNumber().equals(oldNumber) && bankCardService.findBankCardByNumber(bankCard.getCardNumber()) != null) {
            addFlashMessage(redirectAttributes, Message.error("该银行卡已存在"));
            return "redirect:add.htm";
        }

        String code = BankUtils.validateAndCacheCardInfo(bankCard.getCardNumber());
        if (code == null) {
            addFlashMessage(redirectAttributes, Message.error("无法识别银行卡所属银行名称"));
            return "redirect:edit.htm?id=" + bankCard.getId();
        }
        bankCard.setCode(code.toLowerCase());
        bankCard.setMember(member);
        bankCardService.update(bankCard, "");
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Pageable pageable, ModelMap model) {
        pageable.getFilters().add(Filter.isNotNull("member"));
        pageable.getOrders().add(Order.desc("createDate"));
        model.addAttribute("page", bankCardService.findPage(pageable));
        return "/admin/bank_card/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Message delete(Long[] ids) {
        bankCardService.delete(ids);
        return SUCCESS_MESSAGE;
    }

}
